package nieAS.video {
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.AsyncErrorEvent;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.events.StageVideoEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.media.StageVideo;
	import flash.media.StageVideoAvailability;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import nieAS.html.js;
	import nieAS.net.NieNetStream;
	import nieAS.utils.ObjectUtils;
	import nieAS.utils.Tracer;
	
	public class BackgroundVideo extends Sprite {
		private var video:StageVideo;
		private var normalVideo:Video;
		private var stream:NieNetStream;
		private var connect:NetConnection;
		private var infoObject:Object = new Object();
		private var rc:Rectangle;
		private var completeCallBack:Function;
		private var _Root:Sprite;
		private var _width:int;
		private var _height:int;
		private var _stageWidth:int;
		private var _autoPlayBack:Boolean;
		private var _showTestData:Boolean;
		private var _bg:Shape;
		private var _rate:Number;
		
		public var isPlaying:Boolean = false;
		public var videoUri:String;
		
		public static const VIDEO_PLAY_START:String = "video_playStart";
		
		public function BackgroundVideo(_root:Sprite, $width:int = 1680, $height:int = 945, $StageWidth:int = 1000, $playBack:Boolean = false, $testData:Boolean = false):void {
			_Root = _root;
			_width = $width;
			_height = $height;
			_stageWidth = $StageWidth;
			_autoPlayBack = $playBack;
			_showTestData = $testData;
			
			//_rate = $width / $height;
			//_bg = drawBackGround();
			//_root.stage.addChildAt(_bg, 0);
			
			//_root.stage.addEventListener(Event.RESIZE, bgResize);
			//bgResize();
			
			connect = new NetConnection();
			connect.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
		}
		
		private function bgResize(evt:Event = null):void {
			_width = _Root.stage.stageWidth;
			_height = _Root.stage.stageHeight;
			var _r:Number = _width / _height;
			if (_r > _rate) {
				_bg.width = _height * _rate;
				_bg.height = _height;
			}else {
				_bg.width = _width;
				_bg.height = _width / _rate;
			}
			_bg.x = (_width - _stageWidth) / 2;
			resize();
			Trace(_bg.width, _bg.height);
		}
		
		public function complete():void {
			if (completeCallBack != null) {
				completeCallBack();
			}
			completeCallBack = null;
		}
		
		public function pause():void {
			if (stream != null) {
				stream.pause();
			}
		}
		
		public function resume():void {
			if (stream != null) {
				stream.resume();
			}
		}
		
		public function seek(_num:Number):void {
			if (stream != null) {
				stream.seek(_num);
			}
		}
		
		public function play(_uri:String, _callBack:Function = null):void {
			videoUri = _uri;
			completeCallBack = _callBack;
			isPlaying = true;
			if (stream != null) {
				stream.play(videoUri);
			}else {
				connect.connect(null);
			}
		}
		
		private function createStream():void {
			stream = new NieNetStream(connect);
			infoObject.onMetaData = onMetaData;
			infoObject.onPlayStatus = onPlayStatus;
			stream.client = infoObject;
			stream.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
			stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
			createVideo();
		}
		
		private function Trace(...rest):void {
			if (_showTestData) {
				Tracer.Trace(rest);
			}
		}
		
		private function createVideo():void {
			/*if (js.getBrowserType().indexOf("msie") != -1) {
				if (StageVideoAvailability.AVAILABLE && _Root.stage.stageVideos.length > 0) {
					video = _Root.stage.stageVideos[0];
					video.addEventListener(StageVideoEvent.RENDER_STATE, stageVideoStateChange);
					video.attachNetStream(stream);
					Trace("使用stageVideo:true");
					stream.play(videoUri);
					return;
				}
			}*/
			normalVideo = new Video(_width, _height);
			normalVideo.attachNetStream(stream);
			_Root.stage.addChildAt(normalVideo, 0);
			//resize();
			Trace("使用stageVideo:false");
			stream.play(videoUri);
		}
		
		private function stageVideoStateChange(evt:StageVideoEvent):void {
			var _statue:String = evt.status;
			Trace(_statue);
			resize();
		}
		
		public function resize():void {
			Trace("stageVideo Resize");
			/*if (video != null) {
				rc = new Rectangle( -340, _num == 0?0: -150, _width, _height);
				video.viewPort = rc;
			}*/
			if (normalVideo != null) {
				//normalVideo.x = _bg.x;
				normalVideo.x = -340;
				//normalVideo.width = _bg.width;
				//normalVideo.height = _bg.height;
			}
		}
		
		private function drawBackGround():Shape {
			var _shape:Shape = new Shape();
			_shape.graphics.beginFill(0, 0);
			_shape.graphics.drawRect(0, 0, _width, _height);
			_shape.graphics.endFill();
			return _shape;
		}
		
		private function onMetaData(_infoObject:Object):void {
			if (_showTestData) {
				//Trace("onMetaData: ");
				//ObjectUtils.traceObject(_infoObject);
			}
		}
		
		private function onPlayStatus(_infoObject:Object):void {
			if (_showTestData) {
				//Trace("onPlayStatus: ");
				//ObjectUtils.traceObject(_infoObject);
			}
			switch(_infoObject.code) {
				case "NetStream.Play.Complete":
					Trace("播放完毕");
					if (_autoPlayBack) {
						stream.seek(0);
					}
					isPlaying = false;
					complete();
					break;
			}
		}
		
		private function asyncErrorHandler(evt:AsyncErrorEvent):void {
			//Trace("asyncErrorHandler: " + evt);
		}
		
		private function netStatus(evt:NetStatusEvent):void {
			//Trace("NetStatus: " + evt.info.code);
			switch(evt.info.code) {
				case "NetStream.Play.Start":
					_Root.dispatchEvent(new Event(VIDEO_PLAY_START));
					break;
				case "NetConnection.Connect.Success":
					createStream();
					break;
				case "NetStream.Buffer.Full":
					resize();
					break;
			}
		}
	}
}